package com.jinghao.tws.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import com.jinghao.core.common.exception.JingHaoCustomerException;
import com.jinghao.core.common.service.impl.JingHaoBaseServiceImpl;
import com.jinghao.core.common.util.cnd.JhCnd;
import com.jinghao.tws.service.IWvFlowService;
import com.jinghao.tws.service.IWvProcessMergeConfigService;
import com.jinghao.tws.util.EJhTwsCode;
import com.jinghao.tws.entity.WvAutoVedioConfig;
import com.jinghao.tws.entity.WvFlow;
import com.jinghao.tws.entity.WvProcessMergeConfig;

@Service("wvProcessMergeConfigService")
public class WvProcessMergeConfigServiceImpl extends JingHaoBaseServiceImpl<WvProcessMergeConfig> implements IWvProcessMergeConfigService {
	//==================定制内容开始======================
//在这里修改代码
	@Resource
	private IWvFlowService wvFlowService;
	@Override
	public void listWvProcessMergeConfig(WvProcessMergeConfig model) {
		if (StringUtils.isEmpty(model.getFlowUuid())){
			throw new JingHaoCustomerException(EJhTwsCode.TWS9001.getCode(),"流程UUID不能为空，请重新输入!");
		}
		WvFlow wvFlow=new WvFlow();
		wvFlow.setFlowUuid(model.getFlowUuid());
		JhCnd cnd=new JhCnd();
		cnd.andEquals("flowUuid");
		wvFlow.setJhCnd(cnd);
		List<Map<String,Object>> list=this.wvFlowService.queryMapListById(wvFlow, "listWvFlowALLUserConfig");
		
		if(list==null||list.size()<=0){
			throw new JingHaoCustomerException(EJhTwsCode.TWS9001.getCode(),"未找到相关流程!");
		}else if(list.size()>1){
			throw new JingHaoCustomerException(EJhTwsCode.TWS9001.getCode(),"找到多条流程信息!");
		}
		
		 Map<String,Object> tempMap=list.get(0);
		 cnd.reset();
		 cnd.andEquals("flowUuid");
		 model.setJhCnd(cnd);
		 List<Map<String,Object>> listConfig=this.queryMapListById(model, "listWvProcessMergeConfigToCode");
		 tempMap.put("processList", listConfig);
		 model.setMapList(list);
	}
	
	@Override
	public void listAllWvProcessMergeConfig(WvProcessMergeConfig model) {
		if (StringUtils.isEmpty(model.getFlowUuid())){
			throw new JingHaoCustomerException(EJhTwsCode.TWS10001.getCode(),"流程UUID不能为空，请重新输入!");
		}
		 JhCnd cnd=new JhCnd();
		 cnd.andEquals("flowUuid");
		 model.setJhCnd(cnd);
		 List<Map<String,Object>> list=this.queryMapListById(model, "listWvProcessMergeConfigToCode");
		 model.setMapList(list);
	}
	
	public void addWvProcessMergeConfig(WvProcessMergeConfig model) {
		if (StringUtils.isEmpty(model.getFlowCode())){
			throw new JingHaoCustomerException(EJhTwsCode.TWS9001.getCode(),"流程编码不能为空，请重新输入!");
		}
		if (StringUtils.isEmpty(model.getFlowUuid())){
			throw new JingHaoCustomerException(EJhTwsCode.TWS9001.getCode(),"流程UUID不能为空，请重新输入!");
		}
		JhCnd cnd=new JhCnd();
		cnd.andEquals("flowCode");
		model.setJhCnd(cnd);
		this.deleteSame(model);
		
		cnd.reset();
		cnd.andEquals("flowUuid");
		model.setJhCnd(cnd);
		this.deleteSame(model);
		int i=0;
		if (null!=model.getStrList()&&model.getStrList().size()>0) {
			WvProcessMergeConfig wvProcessMergeConfig=null;
			 for(String processCode:model.getStrList()){
				 if(!processCode.trim().equals("")){
					 wvProcessMergeConfig=new WvProcessMergeConfig();
					 wvProcessMergeConfig.setPmcUuid(model.getUUID());
					 wvProcessMergeConfig.setFlowUuid(model.getFlowUuid());
					 wvProcessMergeConfig.setFlowCode(model.getFlowCode());
					 wvProcessMergeConfig.setProcessCode(processCode);
					 wvProcessMergeConfig.setCreator(" ");
					 wvProcessMergeConfig.setCreateTime(new Date());
					 wvProcessMergeConfig.setSort(i++);
					 this.add(wvProcessMergeConfig);
				 }
			 }
		}
	}
	//==================定制内容结束======================
}
